<?php
	require_once('../inc/fen_fns.php');
	sec_session_start();
	
	if(login_check($mysqli, '>', '50') == true) {

		if ($_SESSION['access_level'] == '50') {
			
			$query = $_DB->Query("SELECT faculty_id from fen_members where id = '".$_SESSION['user_id']."'");
			$faculty_id = "and fen_faculty.id = ".$_DB->GetResultValue($query, "0", "faculty_id");
			
		} else {
			if ($_POST['faculty_id']) { $faculty_id = "and fen_faculty.id = ".$_POST['faculty_id']; } else { $faculty_id = ""; }
		}
		
		
		if ($_POST['department_id']) { $department_id = "and fen_department.id = ".$_POST['department_id']; } else { $department_id = ""; }
		if ($_POST['eser_adi']) { $eser_adi = "and fen_publications.eser_adi LIKE '%".$_POST['eser_adi']."%'"; } else { $eser_adi = ""; }
		if ($_POST['name']) { $name = "and fen_members.name LIKE '%".$_POST['name']."%'"; } else { $name = ""; }
		if ($_POST['surname']) { $surname = "and fen_members.surname LIKE '%".$_POST['surname']."%'"; } else { $surname = ""; }
		if ($_POST['user_id']) { $user_id = "and fen_members.id = ".$_POST['user_id']; } else { $user_id = ""; }
			
		if ($_POST['yayin_tipi']) { $yayin_tipi = "and fen_publications.yayin_tipi = '".$_POST['yayin_tipi']."'"; } else { $yayin_tipi = ""; }
		if ($_POST['yayin_turu']) { $yayin_turu = "and fen_publications.yayin_turu = '".$_POST['yayin_turu']."'"; } else { $yayin_turu = ""; }
		if ($_POST['yayin_yili']) { $yayin_yili = "and fen_publications.yayin_yili = '".$_POST['yayin_yili']."'"; } else { $yayin_yili = ""; }
		
		$yayin_yili = "";
		
		if ($_POST['yayin_yili_baslangic'] && $_POST['yayin_yili_bitis']) {
			if ($_POST['yayin_yili_baslangic'] == $_POST['yayin_yili_bitis']) { $yayin_yili = "and fen_publications.yayin_yili = '".$_POST['yayin_yili_baslangic']."'";
			} else {
				$yayin_yili = "and fen_publications.yayin_yili BETWEEN '".$_POST['yayin_yili_baslangic']."' AND '".$_POST['yayin_yili_bitis']."'";
			} 
		}
		
		
		if ($_POST['indeks_turu']) {	
			if ($_POST['indeks_turu'] == 'Diger') {
				$indeks_turu = "and fen_publications.indeks_turu NOT LIKE '%SCI' and fen_publications.indeks_turu NOT LIKE '%SCIE' and fen_publications.indeks_turu NOT LIKE '%SSCI' and fen_publications.indeks_turu NOT LIKE '%AHCI' and fen_publications.indeks_turu NOT LIKE '%Alan'";
			} else {
				$indeks_turu = "and fen_publications.indeks_turu = '".$_POST['indeks_turu']."'";
			}
		} else {
			$indeks_turu = "";
		}
		
		if ($_POST['sinif']) {
			if ($_POST['sinif'] == 'Diger') {
				$sinif = "and fen_publications.sinif NOT LIKE '%A' and fen_publications.sinif NOT LIKE '%B' and fen_publications.sinif NOT LIKE '%C' and fen_publications.sinif NOT LIKE '%D'";
			} else {
				$sinif = "and fen_publications.sinif = '".$_POST['sinif']."'";
			}
		} else {
			$sinif = "";
		}
		
	
		if ($_POST['user_id']) {
			$query = $_DB->Query("SELECT fen_members.name, fen_members.surname, fen_members.titr, fen_faculty.faculty_name, fen_department.department_name FROM fen_members, fen_faculty, fen_department where fen_members.id = '".$_POST['user_id']."' and fen_members.faculty_id = fen_faculty.id and fen_members.department_id = fen_department.id");
			$tablehead = $_DB->GetResultValue($query, "0", "titr")." ".$_DB->GetResultValue($query, "0", "name")." ".$_DB->GetResultValue($query, "0", "surname")." Yayınları";
		} else {
			$tablehead = "Rapor";
		}
	

?>

	<script type="text/javascript">


var oTable;

$(document).ready(function() {

    $(".contentTopArea").hide();

    $('.buttonAdd').click(function(){
    	$(".contentTopArea").slideToggle(150);
    });

	
    oTable = $('#example').dataTable( {
		"sPaginationType": "full_numbers",
		"sDom": 'f<"toolbar">Trtlip',
		"oTableTools": {
			"aButtons": [
				"xls"
			]
		}
	} );

	$("div.toolbar").html('');
	
});

function showUser(obj) {  

	jQuery(document).ready(function($) {
		  $('a[rel*=facebox]').facebox() 

		  jQuery.facebox({ ajax: 'reports/publication-detailed.php?pid='+obj })
	}) 
} 

function showHistory(obj) {  

	jQuery(document).ready(function($) {
		  $('a[rel*=facebox]').facebox() 

		  jQuery.facebox({ ajax: 'forms/transaction-history.php?pid='+obj })
	}) 
} 

</script>	
	<br><br><button class="buttonAdd" id="buttonAdd" type="button">Yeni Rapor Al</button><br><br><br>
	
			<div class="showtable">
				<div id="summaryTableBigTopp">
					<?php echo $tablehead; ?>
				</div>
			<form id="form">
				<table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
						<thead>
							<tr>
								
								<th>#</th>
								<th>Fakülte</th>
								<th>Bölüm</th>
								<th>Ünvan</th>
								<th>Ad</th>
								<th>Soyad</th>
								<th>Yayın Tipi</th>
								<th>Yayın Türü</th>
								<th>Yayın Yılı</th>
								<th>Volume</th>
								<th>Issue</th>
								<th>Eser Adı</th>
								<th>İndeks</th>
								<th>Grup</th>
								<th>Detay</th>
								<th>Tarihçe</th>
							</tr>
						</thead>
						<tbody>
						
<?php 
						$query = $_DB->Query("SELECT
											fen_members.titr as titr,
											fen_members.name as name,
											fen_members.surname as surname,
											fen_members.username as username,
											fen_department.department_name as department_name,
											fen_faculty.faculty_name as faculty_name,
											fen_publications.id as id,
											fen_publications.eser_adi as publication_name,
											fen_publications.indeks_turu as indeks_turu,
											fen_publications.yayin_tipi as yayin_tipi,
											fen_publications.yayin_turu as yayin_turu,
											fen_publications.yayin_yili as yayin_yili,
											fen_publications.volume as yayin_volume,
											fen_publications.issue as yayin_issue,
											fen_publications.indeks_turu as indeks_turu,
											fen_publications.sinif as sinif,
											fen_publications.eser_adi as eser_adi,
											fen_publications.id as publication_id
											from fen_members, fen_department, fen_faculty, fen_publications
											WHERE
											fen_publications.member_id = fen_members.id and
											fen_members.department_id = fen_department.id and
											fen_members.faculty_id = fen_faculty.id
											$faculty_id
											$department_id
											$eser_adi
											$name
											$surname
											$yayin_tipi
											$yayin_turu
											$yayin_yili
											$indeks_turu
											$sinif
											$user_id
											ORDER BY fen_publications.yayin_yili DESC");

						$count = $_DB->GetResultNumber($query);
						
						$date = date("d F Y",strtotime($myrow['date']));

						for ($i=0; $i<$count; $i++)
						{
							
							$no = $i + 1;
							
							/*
							if ($_DB->GetResultValue($query, $i, "titr") == 'Prof. Dr.') {
								$titr = "<p id=\"hiddentitr\">1</p>Prof. Dr.";
							} else if ($_DB->GetResultValue($query, $i, "titr") == 'Doç. Dr.') {
								$titr = "<p id=\"hiddentitr\">2</p>Doç. Dr.";
							} else if ($_DB->GetResultValue($query, $i, "titr") == 'Yard. Doç. Dr.') {
								$titr = "<p id=\"hiddentitr\">3</p>Yard. Doç. Dr.";
							} else if ($_DB->GetResultValue($query, $i, "titr") == 'Öğr. Gör. Dr.') {
								$titr = "<p id=\"hiddentitr\">4</p>Öğr. Gör. Dr.";
							} else if ($_DB->GetResultValue($query, $i, "titr") == 'Öğr. Gör.') {
								$titr = "<p id=\"hiddentitr\">5</p>Öğr. Gör.";
							} else if ($_DB->GetResultValue($query, $i, "titr") == 'Uzm.') {
								$titr = "<p id=\"hiddentitr\">6</p>Uzm.";
							} else if ($_DB->GetResultValue($query, $i, "titr") == 'Okutman') {
								$titr = "<p id=\"hiddentitr\">7</p>Okutman";
							} else if ($_DB->GetResultValue($query, $i, "titr") == 'Arş. Gör.') {
								$titr = "<p id=\"hiddentitr\">8</p>Arş. Gör.";
							}
							*/
							
							$titr = $_DB->GetResultValue($query, $i, "titr");
							
							echo "
								<tr>
									<td>".$no."</td>
									<td>".$_DB->GetResultValue($query, $i, "faculty_name")."</td>
									<td>".$_DB->GetResultValue($query, $i, "department_name")."</td>
									<td>".$titr."</td>
									<td>".$_DB->GetResultValue($query, $i, "name")."</td>
									<td>".$_DB->GetResultValue($query, $i, "surname")."</td>
									<td>".$_DB->GetResultValue($query, $i, "yayin_tipi")."</td>
									<td>".$_DB->GetResultValue($query, $i, "yayin_turu")."</td>
									<td>".$_DB->GetResultValue($query, $i, "yayin_yili")."</td>
									<td>".$_DB->GetResultValue($query, $i, "yayin_volume")."</td>
		  							<td>".$_DB->GetResultValue($query, $i, "yayin_issue")."</td>
									<td>".$_DB->GetResultValue($query, $i, "eser_adi")."</td>
									<td>".$_DB->GetResultValue($query, $i, "indeks_turu")."</td>
									<td>".$_DB->GetResultValue($query, $i, "sinif")."</td>
									<td class=\"center\"><a class =\"user_detail_but\" onclick=\"showUser('".$_DB->GetResultValue($query, $i, "id")."')\" href=\"#\"><img id=\"editimg\" src=\"images/edit.gif\"></img></a></td>
									<td class=\"center\"><a class =\"user_detail_but\" onclick=\"showHistory('".$_DB->GetResultValue($query, $i, "publication_id")."')\" href=\"#\"><img id=\"editimg\" src=\"images/transactionhistory.gif\"></img></a></td>
								</tr>";		
						}
?>
						</tbody>
					</table>
					
				</form>
			<br><br>
			</div>
	</div>
	
<?php 

	} else {
		header('Location: ./index.php?error=2');
	}

?>	